package cn.victory._03_redisConnection;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.sentinel.api.StatefulRedisSentinelConnection;
import org.junit.Test;

/**
 * @ Description TODO
 * @ Author victory
 * @ Date 2022/6/29
 **/
public class RedisConnectionDemo {

    /**
    *
    * @ Description  普通连接
    * @ param
    **/
    @Test
    public void testRedisConnection(){
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        //-------代码---------
        //...
        //-------End---------
        //关闭连接
        connect.close();
        //关闭客户端
        redisClient.shutdown();
    }


    /**
    *
    * @ Description  哨兵类型的连接
    * @ param
    **/
    @Test
    public void testRedisSentinelConnection(){
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisSentinelConnection<String, String> stringStringStatefulRedisSentinelConnection = redisClient.connectSentinel();
        //-------代码---------
        //...
        //-------End---------
        //关闭连接
        stringStringStatefulRedisSentinelConnection.close();
        //关闭客户端
        redisClient.shutdown();
    }


    /**
    *
    * @ Description  发布订阅类型的连接
    * @ param
    **/
    @Test
    public void testRedisPubSubConnection(){
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisPubSubConnection<String, String> stringStringStatefulRedisPubSubConnection = redisClient.connectPubSub();
        //-------代码---------
        //...
        //-------End---------
        //关闭连接
        stringStringStatefulRedisPubSubConnection.close();
        //关闭客户端
        redisClient.shutdown();
    }
}
